package b2.widget
{
	import b2.net.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
	import flash.utils.Timer;

	public class BasicIcon extends Sprite
	{
		private var iconImage:Sprite;
		private var oldPosX:Number;
		private var oldPosY:Number;
		//0, 上方；1，下方
		private var tipPosType:int;
		private var tip:BasicMessage;
		private var tipText:String;
		private var oldWidth:Number;
		private var oldHeight:Number;
		
		//用于闪烁的变量
		private var shineFlag:Boolean;
		private var biggerFlag:Boolean;
		private var timer:Timer;
		
		public function BasicIcon(IconImage:Class, tipText:String = null, tipPosType:int = 0, textEmbeddedFlag:Boolean = false, iconBackgroundFlag:Boolean = false, textColor:int = 0x000000)
		{
			super();
			this.tipText = tipText;
			this.tipPosType = tipPosType;
			if (iconBackgroundFlag)
			{
				var IconBackground:Class = ResourceHelper.getInstance().getClassByName("IconBackground");
				iconImage = new IconBackground();
				addChild(iconImage);
				//图标图片
				var icon:Sprite = new IconImage();
				icon.width = 35;
				icon.height = 35;
				icon.x = (iconImage.width - icon.width) / 2;
				icon.y = (iconImage.height - icon.height) / 2;
				icon.mouseEnabled = false;
				iconImage.addChild(icon);
				addChild(iconImage);
			}
			else
			{
				//图标图片
				iconImage = new IconImage();
				addChild(iconImage);
			}
			var tipGap:int = 10;
			//文字嵌入到图片下部
			if (tipText != null && textEmbeddedFlag)
			{
				var embeddedText:TextField = new TextField();
				embeddedText.selectable = false;
	        	embeddedText.autoSize = TextFieldAutoSize.CENTER;
	        	if (tipText.length < 5)
					embeddedText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
				else
	        		embeddedText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0xFFFFFF);
				embeddedText.filters = [new GlowFilter(textColor, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				embeddedText.text = tipText;
				embeddedText.x = (iconImage.width - embeddedText.width) / 2;
				if (tipPosType == 0)
					embeddedText.y = -embeddedText.height;
				else
					embeddedText.y = iconImage.height;
				iconImage.addChild(embeddedText);
			}
			oldPosX = iconImage.x;
			oldPosY = iconImage.y;
			oldWidth = iconImage.width;
			oldHeight = iconImage.height;
			shineFlag = false;
			biggerFlag = false;
			this.mouseChildren = false;
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (!shineFlag)
			{
				var scale:Number = 1.2;
				TweenLite.to(iconImage, 0.2, {scaleX:scale, scaleY:scale, x:oldPosX - (scale - 1) * oldWidth / 2, y:oldPosY - (scale - 1) * oldHeight / 2});
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (!shineFlag)
			{
				TweenLite.to(iconImage, 0.2, {scaleX:1, scaleY:1, x:oldPosX, y:oldPosY});
			}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			stopShine();	
		}
		
		public function startShine(color:int = 0xEECF1F):void
		{
			if (!shineFlag)
			{
				var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
				gradientGlow.distance = 0;
				gradientGlow.angle = 45;
				gradientGlow.colors = [0xFFFFFF, color];
				gradientGlow.alphas = [0, 1];
				gradientGlow.ratios = [0, 255];
				gradientGlow.blurX = 10;
				gradientGlow.blurY = 10;
				gradientGlow.strength = 4;
				gradientGlow.quality = BitmapFilterQuality.HIGH;
				gradientGlow.type = BitmapFilterType.OUTER;
				iconImage.filters = [gradientGlow];
				shineFlag = true;
				
				timer = new Timer(200);
				timer.start();
				timer.addEventListener(TimerEvent.TIMER, onTick);
			}
		}
		
		public function stopShine():void
		{
			if (shineFlag)
			{
				iconImage.filters = null;
				shineFlag = false;
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onTick);
			}
		}
		
		private function onTick(event:Event):void
		{
			if (shineFlag)
			{
				if (biggerFlag)
				{
					var scale:Number = 0.8;
					iconImage.x = oldPosX - (scale - 1) * oldWidth / 2;
					iconImage.y = oldPosY - (scale - 1) * oldHeight / 2;
					iconImage.scaleX = scale;
					iconImage.scaleY = scale;
				}
				else
				{
					iconImage.x = oldPosX;
					iconImage.y = oldPosY;
					iconImage.scaleX = 1;
					iconImage.scaleY = 1;
				}
				biggerFlag = !biggerFlag;
			}
		}
	}
}